package top.houry.queue;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class DelayQueueTest {
    public static void main(String[] args) throws InterruptedException {
//        DelayQueue  delayQueue = new DelayQueue();
//
//        MyDelayTask task1 = new MyDelayTask("task1", 10000);
//        MyDelayTask task2 = new MyDelayTask("task2",3900);
//        MyDelayTask task3 = new MyDelayTask("task3",1900);
//        IntStream.range(0, 1).forEach(value -> new Thread(() ->{
//            delayQueue.offer(task1);
//            delayQueue.offer(task2);
//            delayQueue.offer(task3);
//            delayQueue.offer(new MyDelayTask("task4",5900));
//            delayQueue.offer(new MyDelayTask("task5",6900));
//            delayQueue.offer(new MyDelayTask("task6",7900));
//            delayQueue.offer(new MyDelayTask("task7",4900));
//        }).start());
//
//        TimeUnit.SECONDS.sleep(1);
//        boolean remove = delayQueue.remove(task2);
//        System.out.println(remove);
////        TimeUnit.SECONDS.sleep(5);
//        while (true) {
//            Delayed take = delayQueue.take();
//            System.out.println(take);
//        }

        System.out.println(getChineseShowTime(119));

    }
    private static String getChineseShowTime(int totalSeconds) {
        if (0 == totalSeconds) {
            return "0分";
        }
        int h = totalSeconds / 3600;
        double m = (totalSeconds % 3600) / 60.0;
        if (h > 0) {
            return h + "时" + m + "分";

        } else {
//            if (m == 0) {
//                return "1分";
//            } else {
//                return m + "分";
//            }
            return (int)Math.ceil(m) +"分";
        }

    }



    static class MyDelayTask implements Delayed {
        private String name ;
        private long start = System.currentTimeMillis();
        private long time ;


        public MyDelayTask(String name,long time) {
            this.name = name;
            this.time = time;
        }


        @Override
        public long getDelay(TimeUnit unit) {
            return unit.convert((start+time) - System.currentTimeMillis(),TimeUnit.MILLISECONDS);
        }

        @Override
        public int compareTo(Delayed o) {
            return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
        }

        @Override
        public String toString() {
            return "MyDelayTask{" +
                    "name='" + name + '\'' +
                    ", start=" + start +
                    ", time=" + time +
                    '}';
        }
    }
}
